package com.wnnight.wys.mapper.config;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnnight.wys.entity.config.ChargeConfig;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @ClassName: ChargeConfigMapper
 * @Description: //收费配置mapper
 * @author: LZJ
 * @date: 2022/8/28-10:46
 * @version: v1.0
 */
public interface ChargeConfigMapper extends BaseMapper<ChargeConfig> {
    @SelectProvider(type = sqlProvider.class, method = "selectByCondition")
    List<ChargeConfig> getByCondition(ChargeConfig chargeConfig);

    @UpdateProvider(type = sqlProvider.class, method = "update")
    void update(ChargeConfig chargeConfig);

    //收费配置mapper动态sql
    class sqlProvider {
        //修改
        public static String update(ChargeConfig chargeConfig) {
            String sql = new SQL() {
                {
                    UPDATE("wys_charge_config");
                    if (chargeConfig.getChargeProject() != null) {
                        SET("charge_project=#{chargeProject}");
                    }
                    if (chargeConfig.getUnitPrice() != null) {
                        SET("unit_price=#{unitPrice}");
                    }
                    if (chargeConfig.getCompany() != null) {
                        SET("company=#{company}");
                    }
                    if (chargeConfig.getMessage() != null) {
                        SET("message=#{message}");
                    }
                    if (chargeConfig.getStatus() != null) {
                        SET("status=#{status}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            return sql;
        }

        //分页查询
        public static String selectByCondition(ChargeConfig chargeConfig) {
            System.out.println(chargeConfig.getChargeProject());
            String sql = new SQL() {
                {
                    SELECT("*");
                    FROM("wys_charge_config");
                    if (chargeConfig.getChargeProject() != null) {
                        WHERE("charge_project like '%" + chargeConfig.getChargeProject() + "%'");
                    }
                    if (chargeConfig.getUnitPrice() != null) {
                        WHERE("unit_price=#{unitPrice}");
                    }
                    if (chargeConfig.getCompany() != null) {
                        WHERE("company=#{company}");
                    }
                    if (chargeConfig.getMessage() != null) {
                        WHERE("message=#{message}");
                    }
                    if (chargeConfig.getStatus() != null) {
                        WHERE("status=#{status}");
                    }
                }
            }.toString();
            return sql;
        }
    }
    @Insert("insert into wys_charge_config(charge_project,unit_price,company,message,status)values(#{chargeProject},#{unitPrice},#{company},#{message},#{status})")
    void add(ChargeConfig chargeConfig);
}
